package com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.robertluozizhao.framecollectdemo.R
import com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.bean.Friend
import com.example.robertluozizhao.framecollectdemo.activity.test.rongcloud.bean.FriendTitle
import java.util.*


/**
 * Created by Lzz on 11/29/2020.
 */
class FriendRvAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>()/*, SectionIndexer*/{
    private var list: List<Any> = mutableListOf()

    var mQunzhu: Map<Friend, Int> = HashMap()
    var mManager = LinkedHashMap<Friend, Int>()
    var mMember = LinkedHashMap<Friend, Int>()
    var letterIndex: Map<String, Int> = HashMap() //letter index
    var letterCount: Map<String, Int> = HashMap() //letter count


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when(viewType){
            100 ->{
                return ViewItemHolder(LayoutInflater.from(parent.context).inflate(R.layout.friend_item, parent, false));
            }
        }

        return  ViewTittleHolder(LayoutInflater.from(parent.context).inflate(R.layout.friend_title, parent, false));
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(list[position]){
            is FriendTitle->{
                val friendTitle = list[position] as FriendTitle
                if(friendTitle.letters =="群主"){
                    friendTitle.name = "群主管理员 ${mManager.size+1}"
                }else{
                    friendTitle.name = "${friendTitle.letters} 成员 ${letterCount[friendTitle.letters]} 人"
                }
                (holder as ViewTittleHolder).setData(friendTitle)
            }
            is Friend->{
                (holder as ViewItemHolder).setData(list[position] as Friend)
            }
        }
    }




    override fun getItemCount(): Int {
        return list.size
    }



    fun MoveToPosition(mRecyclerView: RecyclerView, n: Int) {
        val firstItem = (mRecyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
        val lastItem =  (mRecyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
        if (n <= firstItem) {
            mRecyclerView.scrollToPosition(n)
        } else if (n <= lastItem) {
            val top = mRecyclerView.getChildAt(n - firstItem).top
            mRecyclerView.scrollBy(0, top)
        } else {
            mRecyclerView.scrollToPosition(n)
        }
    }


    fun getLetterTittle(letter: String):Int{

        for (item in 0 until itemCount){
            if (list[item] is FriendTitle) {
                val friendTitle = list[item] as FriendTitle
                if (friendTitle.letters == letter) {
                    return item
                }
            }
        }

        return -1
    }


//    override fun getSections(): Array<Any?>? {
//        return arrayOfNulls(0)
//    }

//    override fun getPositionForSection(sectionIndex: Int): Int {
//        for (i in 0 until itemCount) {
//            if (list.get(i).getType() == 0) {
//                val sortStr: String = list.get(i).getLetters()
//                val firstChar = sortStr.toUpperCase()[0]
//                if (firstChar.toInt() == sectionIndex) {
//                    return i
//                }
//            }
//        }
//        return -1
//    }

//    override fun getSectionForPosition(position: Int): Int {
//        if (list[position] is FriendTitle){
//           return (list[position] as FriendTitle).index
//        }
//        return -1
//    }


    override fun getItemViewType(position: Int): Int {
        when (list[position]) {
            is Friend->{
                return 100
            }
            is FriendTitle->{
                return 200
            }
        }
        return super.getItemViewType(position)
    }

    fun setList(list: List<Any>) {
        this.list = list
        notifyDataSetChanged()
    }

    internal class ViewItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

        /**
         * 首字母
         */
        var tvLetter: TextView? = null

        /**
         * 昵称
         */
        var tvTitle: TextView? = null

        /**
         * 头像
         */
        var mImageView: ImageView? = null
        /**
         * userid
         */
        //        TextView tvUserId;
        init {
            tvTitle = itemView.findViewById(R.id.friendname)
        }

        fun  setData(friend: Friend){
            tvTitle?.text = friend.name
        }

    }

    internal class ViewTittleHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

        /**
         * 首字母
         */
        var tvLetter: TextView? = null

        /**
         * 昵称
         */
        var catalog: TextView? = null

        init {
            catalog = itemView.findViewById(R.id.catalog)
        }

        fun  setData(count: FriendTitle){
            catalog?.text = "${count.name}"
        }

    }
}